package com.kx.smartdevice.controller;

import com.github.pagehelper.PageInfo;
import com.kx.smartdevice.dto.R;
import com.kx.smartdevice.pojo.Category;
import com.kx.smartdevice.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Set;

/**
 * @ClassName CategoryController
 * @Author Sun
 * @Date 2021/5/27 21:49
 */
@RestController
@RequestMapping("/categories")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/categories")
    public R getAllCategories() {
        //从缓存中获取数据
        Set<Category> categorySet = categoryService.findAllCategories();
        ArrayList<Category> arrayList = new ArrayList<>();
        arrayList.addAll(categorySet);
        PageInfo<Category> pageInfo = new PageInfo<Category>(arrayList);
        pageInfo.setTotal(arrayList.size());

        return R.setOK(pageInfo);
    }

    @PutMapping("/category")
    public R addCategory(@RequestBody Category category) {
        categoryService.addCategory(category);
        return R.setOK(null);
    }

    @DeleteMapping("/categories")
    public R deleteCategory(String[] ids) {
       int result = categoryService.deleteCategory(ids);
        return R.setOK(result);
    }

    @PostMapping("/category")
    public R updateUser(@RequestBody Category category){
        int result = categoryService.updateCategory(category);
        return R.setOK(result);
    }
}
